package com.distribution.system.domain.converter;

import com.distribution.system.domain.bo.DefaultProfitConditionBO;
import com.distribution.system.domain.bo.RefreshOrderBO;
import com.distribution.system.domain.dto.PlaceOrderDTO;
import com.distribution.system.domain.entity.CmccDaProduct;
import com.distribution.system.domain.entity.CmccDaOrder;
import com.distribution.system.manager.attributioncode.AttributionCodeMateManager;
import com.distribution.system.manager.zoningcode.ZoningCodeMateManager;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.Objects;


@Mapper(componentModel = "spring", imports = {LocalDateTime.class, BigDecimal.class, Objects.class})
public interface UnOrderOperationServiceConverter {
    @Mapping(target = "productCode", source = "cmccDaProduct.productCode")
    @Mapping(target = "productParam", source = "cmccDaProduct.productParam")
    @Mapping(target = "productName", source = "cmccDaProduct.productName")
    @Mapping(target = "productTypeId", source = "cmccDaProduct.productTypeId")
    @Mapping(target = "productImg", source = "cmccDaProduct.mainImagePath")
    @Mapping(target = "bindChannelInfoId", source = "cmccDaProduct.bindChannelInfoId")
    @Mapping(target = "createTime", expression = "java(LocalDateTime.now())")
    @Mapping(target = "updateTime", expression = "java(LocalDateTime.now())")
    @Mapping(target = "scMoney", expression = "java(BigDecimal.ZERO)")
    @Mapping(target = "productStatus", ignore = true)
    @Mapping(target = "addressProvince", source = "placeOrderDTO.addressProvince")
    @Mapping(target = "addressProvinceCode", source = "placeOrderDTO.addressProvinceCode")
    @Mapping(target = "addressCity", source = "placeOrderDTO.addressCity")
    @Mapping(target = "addressCityCode", source = "placeOrderDTO.addressCityCode")
    @Mapping(target = "addressArea", source = "placeOrderDTO.addressDistrict")
    @Mapping(target = "addressAreaCode", source = "placeOrderDTO.addressDistrictCode")
    @Mapping(target = "address", expression = "java(getAddress(placeOrderDTO.getAddressProvince(), placeOrderDTO.getAddressCityCode(), placeOrderDTO.getAddressDistrict(), placeOrderDTO.getAddressStreet()))")
    @Mapping(target = "attributeProvince", source = "placeOrderDTO.attributeProvince")
    @Mapping(target = "attributeProvinceCode", source = "placeOrderDTO.attributeProvinceCode")
    @Mapping(target = "attributeCity", source = "placeOrderDTO.attributeCity")
    @Mapping(target = "attributeCityCode", source = "placeOrderDTO.attributeCityCode")
    CmccDaOrder placeOrderDTOToDo(PlaceOrderDTO placeOrderDTO, CmccDaProduct cmccDaProduct, ZoningCodeMateManager zoningCodeMateManager, AttributionCodeMateManager attributionCodeMateManager);

    default String getAddress(String addressProvince, String addressCity, String addressDistrict, String addressStreet){
        return addressProvince + addressCity + addressDistrict + addressStreet;
    }

    @Mapping(target = "productStatus", expression = "java(refreshOrderBo.getProductStatus())")
    @Mapping(target = "scMoney", expression = "java(Objects.isNull(refreshOrderBo.getScMoney()) ? \"0\" : refreshOrderBo.getScMoney())")
    @Mapping(target = "status", expression = "java(refreshOrderBo.getStatus())")
    DefaultProfitConditionBO refreshOrderBOToDefaultProfitConditionBO(RefreshOrderBO refreshOrderBo);
}
